/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package east2west.dao;

import east2west.dto.CarTypesDTO;
import east2west.interfaces.CarTypeInterface;
import east2west.util.DatabaseUtil;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author anhtuan
 */
public class CarTypeDAO implements CarTypeInterface {

    Connection con = null;
    CarTypesDTO cartype = null;
    CallableStatement call = null;
    Statement st = null;
    ResultSet rs = null;
    private final String SQL_READ_BY_ALL = "select * from V_CarTypes";
    private final String SQL_READ_BY_ID = "{call sp_Call_View_CarTypes_FindId (?)}";
    private final String SQL_READ_BY_NAME = "{call sp_Call_View_CarTypes_FindName (?)}";
    private final String SQL_CREATE = "{call sp_Insert_CarTypes (?)}";
    private final String SQL_UPDATE = "{call sp_Update_CarTypes (?,?)}";
    private final String SQL_DELETE = "{call sp_Delete_CarTypes (?)}";

    public ArrayList<CarTypesDTO> readByAll() {
        ArrayList<CarTypesDTO> list = null;
        try {
            con = DatabaseUtil.getConnection();
            st = con.createStatement();
            rs = st.executeQuery(SQL_READ_BY_ALL);
            list = new ArrayList<CarTypesDTO>();
            while (rs.next()) {
                cartype = new CarTypesDTO();
                cartype.setTypeId(rs.getInt(1));
                cartype.setTypeName(rs.getString(2));
                cartype.setIsDeleted(rs.getByte(3));
                list.add(cartype);
            }
            rs.close();
            st.close();
            return list;
        } catch (Exception ex) {
            return null;
        } finally {
            DatabaseUtil.closeConnection(con);
        }
    }

    public CarTypesDTO findByid(int id) {
        try {
            con = DatabaseUtil.getConnection();
            call = con.prepareCall(SQL_READ_BY_ID);
            call.setInt(1, id);
            rs = call.executeQuery();

            while (rs.next()) {
                cartype = new CarTypesDTO();
                cartype.setTypeId(rs.getInt(1));
                cartype.setTypeName(rs.getString(2));
                cartype.setIsDeleted(rs.getByte(3));

            }
            rs.close();
            call.close();
            return cartype;
        } catch (Exception ex) {
            return null;
        } finally {
            DatabaseUtil.closeConnection(con);
        }
    }

    public ArrayList<CarTypesDTO> findByName(String name) {
        ArrayList<CarTypesDTO> list = null;
        try {
            con = DatabaseUtil.getConnection();
            call = con.prepareCall(SQL_READ_BY_NAME);
            call.setString(2, name);
            rs = call.executeQuery();
            list = new ArrayList<CarTypesDTO>();
            while (rs.next()) {
                cartype = new CarTypesDTO();
                cartype.setTypeId(rs.getInt(1));
                cartype.setTypeName(rs.getString(2));
                cartype.setIsDeleted(rs.getByte(3));
                list.add(cartype);
            }
            rs.close();
            call.close();
            return list;
        } catch (Exception ex) {
            return null;
        } finally {
            DatabaseUtil.closeConnection(con);
        }
    }

    public boolean create(CarTypesDTO dto) {
        try {
            con = DatabaseUtil.getConnection();
            call = con.prepareCall(SQL_CREATE);
            call.setString(1, dto.getTypeName());
            call.executeUpdate();
            return true;
        } catch (Exception ex) {
            return false;
        } finally {
            try {
                DatabaseUtil.closeConnection(con);
                call.close();
            } catch (SQLException ex) {
                Logger.getLogger(CarTypeDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public boolean update(CarTypesDTO dto) {
        try {
            con = DatabaseUtil.getConnection();
            call = con.prepareCall(SQL_UPDATE);
            call.setInt(1, dto.getTypeId());
            call.setString(2, dto.getTypeName());
            call.executeUpdate();
            return true;
        } catch (Exception ex) {
            return false;
        } finally {
            try {
                DatabaseUtil.closeConnection(con);
                call.close();
            } catch (SQLException ex) {
                Logger.getLogger(CarTypeDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public boolean delete(int id) {
        try {
            con = DatabaseUtil.getConnection();
            call = con.prepareCall(SQL_DELETE);
            call.setInt(1, id);
            call.executeUpdate();
            return true;
        } catch (Exception ex) {
            return false;
        } finally {
            try {
                DatabaseUtil.closeConnection(con);
                call.close();
            } catch (SQLException ex) {
                Logger.getLogger(CarTypeDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public boolean delete(CarTypesDTO dto) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
